from pathlib import Path
import random
import os
import shutil

def creat_file(path):
    # create dir
    target_dir = Path(path) / 'a/b/c/d'
    target_dir.mkdir(parents=True)

    # create file
    cfn = lambda: chr(random.randint(97, 122))
    numb_random = 50
    for i in list(target_dir.parents)[:-1]:
        create_file_numb = random.randint(0, numb_random)
        numb_random -= create_file_numb
        while create_file_numb:
            file_name = i / (cfn() + cfn() + cfn() + cfn())
            with open(file_name, 'w') as f:
                f.write(f.name)
            create_file_numb -= 1
    while numb_random:
        numb_random -= 1
        file_name = target_dir / (cfn() + cfn() + cfn() + cfn())
        with open(file_name, 'w') as f:
            f.write(f.name)

def copy_dir(src, dst, ignoreset):
    for i in Path(src).rglob('*'):
        if i.is_file() and i.name[0] not in ignoreset:
            with open(i, 'r') as file_src:
                datas = file_src.readlines()
                dir = dst / i.parent
                if not dir.exists():
                    #os.makedirs(dir)
                    Path(dir).mkdir(parents=True)
                with open(dst / i, 'w') as file_dst:
                    file_dst.writelines(datas)


if __name__ == '__main__':
    creat_file('./')

    # 自己实现
    copy_dir('./a', './b', {'x', 'y', 'z'})

    # lambda函数
    shutil.copytree('./a', './c', ignore=lambda src, names: {name for name in names if Path(name).name[0] in {'x', 'y', 'z'}})

    # 自带函数
    shutil.copytree('a', 'd', symlinks=True, ignore=shutil.ignore_patterns('x*', 'y*', 'z*'))


# 做得不错
